<?php
namespace Wap\Controller;
use Common\Controller\UsersController;

class InvitationController extends UsersController {

	public function _initialize(){
        parent::_initialize();
        //访问者控制
        if (!$this->visitor->is_login) {
            IS_AJAX && $this->ajaxReturn(0, L('login_please'));
            $this->redirect('user/login');
        }
        $info = $this->visitor->get();
        $this->assign('info', $info);
        
		$this->_sign_mod = D('Sign');
		$this->_user_mod = D('User');
		$this->_SignLog_mod = D('SignLog');
		$this->_ScoreLog_mod = D('ScoreLog');
    }

    public function index(){
    	$data = array(
    		'parentid' => $this->visitor->info['id'],
    	);

    	$res=M('User')->where($data)->limit(20)->select();
    	$this->assign('myuser',$res);
    	$this->getInRank();
    	$this->_config_seo(array(
    		'title' => '邀请好友',
    	));
    	$this->display();
    }
    
    public function getInRank(){
    	//查询我们的 粉丝
    	$this->assign('myincount',M('User')->where(array(
    		'parentid' => $this->visitor->info['id'],
    	))->count());
    	
    	$this->assign('myscore',M('ScoreLog')->where(array(
    		'uid' => $this->visitor->info['id'],
    		'action'=>'wxinvate',
    	))->sum('score'));
    	
    	$_dbpre=C('DB_PREFIX');
    	$res=M('User')->field("count(1) as count,parentid")->where("parentid !=0")->group('parentid')->order("count desc")->select();
    	$this->assign('inrank',$res);
    	
    	//查询记录记录
    	$res=M('ScoreLog')->where(array(
    		'uid'=>$this->visitor->info['id'],
    		'action'=>'wxinvate',
    	))->order("id desc")->select();
    	$this->assign('inhis',$res);
    }
    
	/**
	 * 查询签到记录
	 */
	public function record(){
		$data = array(
    		'uid' => $this->visitor->info['id'],
    	);
		$sign_date=$this->_SignLog_mod->where($data)->order("id desc")->select();
		$this->assign('sign_log',$sign_date);
		$this->display();
	}
}